#include <algorithm>
#include <cctype>
#include <cstdio>
const int N = 300005;
int n;
int a[N], dep[N], f[20][N], diff[N], ans[N];
int e_cnt, heads[N];

struct Edge {
  int v, nxt;
} e[N << 1];

inline void add(int u, int v) { e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt; }

inline int read() {
  int x = 0, ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x;
}

int work(int x, int y) {
  if (x == y) return x;
  if (dep[x] < dep[y]) std::swap(x, y);
  for (int i = 19; ~i; --i) {
    if (dep[f[i][x]] >= dep[y]) x = f[i][x];
  }
  if (x == y) return x;
  for (int i = 19; ~i; --i) {
    if (f[i][x] != f[i][y]) x = f[i][x], y = f[i][y];
  }
  return f[0][x];
}

void dfs1(int x) {
  for (int i = heads[x]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (v == f[0][x]) continue;
    f[0][v] = x;
    dep[v] = dep[x] + 1;
    dfs1(v);
  }
}

void solve(int x) {
  for (int i = heads[x]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (v == f[0][x]) continue;
    solve(v);
    diff[x] += diff[v];
  }
}
int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("squ.in", "r", stdin);
  freopen("squ.out", "w", stdout);
#endif
#endif

  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
  }
  int x, y;
  for (int i = 1; i < n; ++i) {
    x = read(), y = read();
    add(x, y);
    add(y, x);
  }
  f[0][1] = 1;
  dep[1] = 1;
  dfs1(1);
  for (int i = 1; i < 20; ++i) {
    for (int j = 1; j <= n; ++j) {
      f[i][j] = f[i - 1][f[i - 1][j]];
    }
  }
  int lca = work(a[1], a[2]);
  ++diff[a[1]], ++diff[a[2]];
  --diff[lca];
  if (lca != 1) --diff[f[0][lca]];

  for (int i = 2; i < n; ++i) {
    lca = work(a[i], a[i + 1]);
    if (a[i] != 1) ++diff[f[0][a[i]]];
    ++diff[a[i + 1]];
    --diff[lca];
    if (lca != 1) --diff[f[0][lca]];
  }
  solve(1);
  --diff[a[n]];
  for (int i = 1; i <= n; ++i) {
    printf("%d\n", diff[i]);
  }
  return 0;
}